<?php #20071024 http://www.framework.altervista.org/FrameWork/applications/Gallery.inc
define('APPGALLERY','1.00');
/*
 * ImageGallery
 * Copyright (C) 2006 Daniele Deplano
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the license, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */

/*
 * Blocco per VisualFrameWork (http://www.VisualFrameWork.it) di Roberto Viola, Daniele Deplano, Stefano Toxiri
 * che permette di creare automaticamente una galleria di immagini a partire da
 * quelle che ci sono nella directory dove e' presente questo file.
 *
 *
 * Blocco Liberamente tratto da FlatGallery v. 0.3 di Marco Segato
 *

 * Autore    Daniele Deplano  <nost@tiscali.it>
 * Website   http://www.nost.it
 * Versione  0.1
 * Data      18/02/2006
 */

//carica la lingua se esiste
$lang=lang();
switch($lang){
    case "en":
	define("_LOAD","load");
	define("_CARICA_UN_IMMAGINE","load a image ");
	define("_IMMAGINE","image ");
	define("_CARICATA","loaded ");
	define("_ELIMINATA","deleted ");
	define("_RINOMINATA_IN","renamed with ");
	define("_NUOVO_NOME","New name");
	define("_RENAME","Rename ");
	define("_DELETE","Delete");
    break;

    default:
	define("_LOAD","carica");
	define("_CARICA_UN_IMMAGINE","Carica un immagine ");
	define("_IMMAGINE","immagine ");
	define("_CARICATA","caricata ");
	define("_ELIMINATA","eliminata ");
	define("_RINOMINATA_IN","rinominata in ");

	define("_NUOVO_NOME","Nuovo nome");
	define("_RENAME","Rinomina");
	define("_DELETE","Elimina");
	break;
}

?>

<style>

table#gallery
{
 width: 100%;
border-collapse: collapse;
}

table#gallery tr.firstrow
{
background: #EEE;
text-transform: uppercase;
}

table#gallery td
{
width: 33%;
text-align: center;
font-size: 11px;
border: 1px solid #CCC;
}
</style>

<?php structure_openBlock("table appl","Gallery",""); ?>

	<p>


<?php

//-------------------------------------------------------------------------------------------------------


//crea la thumbnails di una foto
function mkthumbs($source,$dest){
    //x:y=64:n
    //n=y*64/n
    list($w,$h)=getimagesize($source);
    if ($w>160){
	$nw=160;
	$nh=160*$h/$w;
    }
    else{
	$nw=$w;
	$nh=$h;
    }
    $thumb=imagecreatetruecolor($nw,$nh);

    if (strtolower(substr($source,-3))=="gif"){
	$foto=imagecreatefromgif($source);
	imagecopyresized($thumb,$foto,0,0,0,0,$nw,$nh,$w,$h);
	imagegif($thumb,$dest);
    }

    if (strtolower(substr($source,-3))=="jpg"){
	$foto=imagecreatefromjpeg($source);
	imagecopyresized($thumb,$foto,0,0,0,0,$nw,$nh,$w,$h);
	imagejpeg($thumb,$dest);
    }

    if (strtolower(substr($source,-4))=="jpeg"){
	$foto=imagecreatefromjpeg($source);
	imagecopyresized($thumb,$foto,0,0,0,0,$nw,$nh,$w,$h);
	imagejpeg($thumb,$dest);
    }

    if (strtolower(substr($source,-3))=="png"){
	$foto=imagecreatefrompng($source);
	imagecopyresized($thumb,$foto,0,0,0,0,$nw,$nh,$w,$h);
	imagepng($thumb,$dest);
    }
}
//-------------------------------------------------------------------------------------------------------

// directory dove sono presenti le immagini
$pic_path = "datas/applications/$apptitle/foto";
$thumb_path = "datas/applications/$apptitle/thumbs";

if(!file_exists("datas/applications/"))mkdir("datas/applications/");
if(!file_exists("datas/applications/$apptitle"))mkdir("datas/applications/$apptitle");
if(!file_exists("datas/applications/$apptitle/foto"))mkdir("datas/applications/$apptitle/foto");
if(!file_exists("datas/applications/$apptitle/thumbs"))mkdir("datas/applications/$apptitle/thumbs");


if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
    ?>
    <hr />
    <form name="gallery_upload" action="index.php?appname=<?=$appname?>&op=upload" enctype='multipart/form-data' method='post'>
    <?=_CARICA_UN_IMMAGINE?><input type="file" name="image"><input type=submit value="<?=_LOAD?>">
    </form>
    <hr />
    <?php
	$op=$_GET['op'];
	if ($op=="upload"){
	    admin_writelog("Gallery:upload");
	    if (move_uploaded_file($_FILES['image']['tmp_name'], $pic_path."/".$_FILES['image']['name'])) {
		//echo $_FILES['image']['name'];
		if (strtolower(substr($_FILES['image']['name'],-4))==".php" )
		    unlink($pic_path."/".$_FILES['image']['name']);
		else{
		    mkthumbs($pic_path."/".$_FILES['image']['name'], $thumb_path."/".$_FILES['image']['name']);
		    echo "<script>alert("._IMMAGINE.$_FILES['image']['name']._CARICATA."');</script>";
		}
	    }
	}
	if ($op=="delimage"){
	    admin_writelog("Gallery:delimage");

	    $image=$_GET['image'];
	    if (file_exists($pic_path."/".$image)){
		unlink($pic_path."/".$image);
		unlink($thumb_path."/".$image);
		echo "<script>alert('"._IMMAGINE."$image "._ELIMINATA."');</script>";
	    }
	}
	if ($op=="renameimage"){

	    $image=$_GET['image'];
	    if (file_exists($pic_path."/".$image)){
		echo"<hr />
		    <form name='gallery_rename' action='index.php?appname=$appname&op=exec_rename' method='post'>
		    <input type='hidden' name='image' value='$image'>
		    "._NUOVO_NOME."<input type='text' name='newname' value='$image'><input type=submit value='"._RENAME."'>
		    </form>
		    <hr />
		";
	    }
	}
	if ($op=="exec_rename"){
	    admin_writelog("Gallery:renameimage");
	    $image=$_POST['image'];
	    $newname=$_POST['newname'];
	    rename($pic_path."/".$image,$pic_path."/".$newname);
	    rename($thumb_path."/".$image,$thumb_path."/".$newname);
	    echo "<script>alert('"._IMMAGINE."$image "._RINOMINATA_IN." $newname');</script>";
	}
    }

// carico tutti i files immagine in un array
$dir = opendir($pic_path);

$n_file = 0;
while ($filename = readdir($dir)){
    //echo substr($filename,-4);
	if(  strtolower(substr($filename,-4))==".gif" || strtolower(substr($filename,-4))==".jpg" || strtolower(substr($filename,-5))==".jpeg" || strtolower(substr($filename,-4))==".png"){
	    $array_dir[$n_file] = $filename;
	    $n_file++;
	}
}


// ordinamento alfabetico dell'array
if (count($array_dir)){
    sort($array_dir);
    $array_dir = array_values($array_dir);
}
?>




<!-- menu_top select per cambio pagina -->
<div align="center">
	<form name="pagine_top" action=""><?=$txt_pagina?>
	<select name="pag_top" onchange="self.location=document.pagine_top.pag_top[document.pagine_top.pag_top.selectedIndex].value">
		<? for($i=0;$i<$n_file/9;$i++)
			{ if(!isset($_GET['pag']))
				echo "<option value=\"index.php?appname=$appname&amp;pag=".($i+1)."\">".($i+1)."</option>";
			else if($_GET['pag'] == $i+1)
				echo "<option value=\"index.php?appname=$appname&amp;pag=".($i+1)."\" selected>".($i+1)."</option>";
				else echo "<option value=\"index.php?appname=$appname&amp;pag=".($i+1)."\">".($i+1)."</option>";
			}
		?>
	</select>
	</form>
</div>

<!-- tabella 9x9 thumbnails -->
<?php if(count($array_dir)){ ?>
<table id="gallery"><tbody>
	<?php if(isset($_GET['pag']))				// determina qual e' la prima foto della pagina: 1,10,19...
		$max_x_pag = ($_GET['pag'] * 9) - 8;
	else $max_x_pag = 1;

	for ($i=$max_x_pag; $i<$max_x_pag+9 and $i<=$n_file; $i++){
	    if($i%3 == 1)					// apertura nuova riga prima delle immagini numero 1,4,7...
		echo "<tr >";

		list($width, $height) = getimagesize($pic_path."/".$array_dir[$i-1]);
		// dimensione massima finestre con immagini: 800x600
		if($width>1000 OR $height>650){
			$width_max = 800;
			$height_max = 600;
			$scrollbars = "yes";
		}
		else {
			$width_max = $width;
			$height_max = $height;
			$scrollbars = "no";
		}
		?>
		<td >	<!-- stampa casella con immagine -->
		    <?php $my_window="MyWindow=window.open('$pic_path/".$array_dir[$i-1]."', 'MyWindow', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=$scrollbars, resizable=no, width=$width_max, height=$height_max, left=10, top=10');" ?>
		  <a href="#" onClick="<?=$my_window ?>"><img border="0" src="<?="$thumb_path/".$array_dir[$i-1]?>" width='160' alt="thumb"></a>
		  <br><b><?=$array_dir[$i-1]?></b>
		  <br><?=round(filesize($pic_path."/".$array_dir[$i-1]) / 1024, 1)." Kb, $width"."x$height"?>
		  <?php  if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){ ?>
		     | <a href="index.php?appname=<?=$appname?>&op=delimage&image=<?=$array_dir[$i-1]?>"><?=_DELETE?></a>
		     | <a href="index.php?appname=<?=$appname?>&op=renameimage&image=<?=$array_dir[$i-1]?>"><?=_RENAME?></a>
		  <?php } ?>

		</td>
		<?
		if($i%3 == 0)					// chiusura della riga dopo le immagini numero 3,6,9...
			echo "</tr>";
		}
	while (($i-1)%3 != 0)					// stampa casella vuota per terminare correttamente la riga
	    { echo "<td width=\"225\" height=\"225\"></td>";
		$i++;
		}
	?>
</tbody></table><br>
<?php } ?>

<!-- menu_bottom select per cambio pagina -->
<div align="center">
	<form name="pagine_bottom" action=""><?=$txt_pagina?>
	<select name="pag_bottom" onchange="self.location=document.pagine_bottom.pag_bottom[document.pagine_bottom.pag_bottom.selectedIndex].value">
		<? for($i=0;$i<$n_file/9;$i++)
			{ if(!isset($_GET['pag']))
				echo "<option value=\"index.php?appname=$appname&amp;pag=".($i+1)."\">".($i+1)."</option>";
			else if($_GET['pag'] == $i+1)
				echo "<option value=\"index.php?appname=$appname&amp;pag=".($i+1)."\" selected>".($i+1)."</option>";
				else echo "<option value=\"index.php?appname=$apptitle&amp;pag=".($i+1)."\">".($i+1)."</option>";
			}
		?>
	</select>
	</form>
	<!-- footer -->
	<br><hr size="1" width="98%">
</div>


	</p>

<?php structure_closeBlock(); ?>
